var app = getApp()
Page({
  data: {
    logInButonDAisable:true,
    account:"",
    psd:"",
    tipShow:false,
    validTip:""
  },
  onLoad: function (options) {
    this.setButtonDisable()
    app.wxlogIn()
    // console.log(app.globalData)
  },
  // 绑定账号输入框
  accountInput:function(e){
    this.setData({
      account: e.detail.value,
       validTip: ''
    })
    this.setButtonDisable()
  },
  //绑定密码输入框
  psdInput:function(e){
    this.setData({
      psd: e.detail.value,
      validTip:''
    })
    this.setButtonDisable()
  },
  //设置登录按钮是否可用
  setButtonDisable:function(){
    if (this.data.account==""||this.data.psd==""){
      this.setData({
        logInButonDAisable: true
      })
    }else{
      this.setData({
        logInButonDAisable: false
      })
    }
  },
  //点击登录按钮时用openId和unionId获取sessionId并缓存本地
  getSessionId:function () {
    var _this = this;
    wx.request({
      url: app.globalData.apiUrl +'/mobileOfficing/api/login',
      method: "POST",
      data: {
        userName: _this.data.account,
        password: _this.data.psd,
        openId: app.globalData.openid,
        unionId: app.globalData.unionid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.result) {
          _this.setData({
            tipShow: false
          })
          //缓存登录名
          wx.setStorageSync("loginUserName", _this.data.account)
          wx.setStorageSync(app.globalData.sessionId, res.data.data)
          wx.navigateBack({
            url: '../index/index'
          })
        } else {
            _this.setData({
              tipShow: true, validTip: res.data.msg
            })
            console.log(_this.data.validTip)
        }
      }
    })
  }
})